package com.yulintu.config;

import com.yulintu.mq.FileMqConstans;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;

/**
 * @author caomin
 * @date 2017/10/28
 * @说明 mq配置
 */
@Configuration
public class AmqpConfig   {

    @Bean
    public Queue getQueue(){
        Queue hello = new Queue(FileMqConstans.FILE_QUEUE_NAME,true);
        return hello;
    }
    @Bean
    public Binding binding2() throws Exception {
        Binding binding= BindingBuilder.bind(getQueue()).to(getExchange());
        return binding;
    }




    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }


    @Bean
    public FanoutExchange getExchange() {
        FanoutExchange fanoutExchange = new FanoutExchange(FileMqConstans.FILE_EXCHANGE_NAME, true, false);
        return fanoutExchange;
    }
}
